function saleAmountInit() {
	var storage = [],
		startDate = null,
		endDate = null,
		data = RENDER.data,
		time = {
			startDate: new Date(startDate),
			endDate: new Date(endDate)
		};

	init();

	function init() {
		if(getDate()) {
			initStorage();
			getAmount();
			clearStorage();
		}
	}

	function initStorage() {
		storage = [];
		for (var i = time.startDate; i <= time.endDate; i = nextMonth(i)) {
			var tmp = {};
			tmp.y = i.getFullYear() + '-' + (i.getMonth() + 1);
			tmp.amount = 0;
			tmp.month = i.getTime();
			storage.push(tmp);
		}
	}

	function getAmount() {
		var list = data.list,
			item, i, j;

		for (i = 0; i < list.length; i++) {
			item = list[i];
			for (j = 0; j < storage.length; j++) {
				mon = storage[j];
				if (item.addDate.time >= mon.month && item.addDate.time < nextMonth(mon.month) && item.isValid) {
					mon.amount += item.summ;
					break;
				}
			}
		}
	}

	function getDate() {
		var reg = /\d\d\/(\d\d\/)?\d\d\d\d$/;
		startDate = document.getElementById('startDate').value;
		endDate = document.getElementById('endDate').value;

		startDate = formatDate(startDate);
		endDate = formatDate(endDate);

		time.startDate = new Date(startDate);
		time.endDate = new Date(endDate);

		if(!startDate.match(reg) || !endDate.match(reg)) {
			return false;
		}
		return true;
	}

	function formatDate(str) {
		var tmp = str.split('/');
		return tmp[1] + '/' + '01' + '/' + tmp[0];

	}

	function clearStorage() {
		for(var i=0; i<storage.length; i++) {
			delete storage[i]['month'];
		}
	}

	function reset() {
		init();
	}

	function nextMonth(d) {
		if (typeof d === 'number') {
			d = new Date(d);
			d = d.setMonth(d.getMonth() + 1);
			return new Date(d).getTime();
		} else {
			d = d.setMonth(d.getMonth() + 1);
			return new Date(d);
		}
	}

	function getStorage() {
		return storage;
	}

	return {
		reset: reset,
		getStorage: getStorage
	};

}

function saleStatistic() {
	var storage = [],
		list = [],
		sum = 0,
		startDate = null,
		endDate = null,
		data = RENDER.data,
		type = 7,
		time = {
			startDate: new Date(startDate),
			endDate: new Date(endDate)
		};

	init();

	function init() {
		if(getDate()) {
			filterData();
			initStorage();
			getAmount();
		}
	}

	function filterData() {
		list = [];
		for(var i=0; i<data.list.length; i++) {
			list.push(data.list[i])
		}
		for(var i=0; i<list.length; i++) {
			var item = list[i];
			if(item.addDate.time < time.startDate.getTime() || item.addDate.time > time.endDate.getTime() || !item.isValid) {
				list.splice(i, 1);
				i--;
			}
		}
	}

	function initStorage() {
		storage = [];
		var tmp = {};
		for (var i=0; i<list.length; i++) {
			for(var j=0; j<list[i].orderItems.length; j++) {
				var orderItem = list[i].orderItems[j],
					product = orderItem.product,
					productType = product.productType;
				if(productType.ptid === type) {
					if(typeof tmp[product.pname] === 'undefined') {
						tmp[product.pname] = product.price * orderItem.count;
					} else {
						tmp[product.pname] += product.price * orderItem.count
					}
				}
			}
		}

		for(var o in tmp) {
			if(tmp.hasOwnProperty(o)) {
				storage.push({
					y: o,
					amount: tmp[o]
				});
			}
		}

		console.log(storage);

	}

	function getAmount() {
		var i;
		for(i=0, sum=0; i<storage.length; i++) {
			sum += storage[i].amount;
		}
		document.getElementById('saleSum').innerText = sum;
	}

	function getDate() {
		var reg = /\d\d\/(\d\d\/)?\d\d\d\d$/;
		startDate = document.getElementById('startDate').value;
		endDate = document.getElementById('endDate').value;
		type = +document.getElementById('productTypeSelect').value;

		startDate = formatDate(startDate);
		endDate = formatDate(endDate);

		time.startDate = new Date(startDate);
		time.endDate = new Date(endDate);

		if(!startDate.match(reg) || !endDate.match(reg)) {
			return false;
		}
		return true;
	}

	function formatDate(str) {
		var tmp = str.split('/');
		return tmp[1] + '/' + tmp[2] + '/' + tmp[0];
	}

	function reset() {
		init();
	}

	function nextDate(d) {
		if (typeof d === 'number') {
			d = new Date(d);
			d = d.setDate(d.getDate() + 1);
			return new Date(d).getTime();
		} else {
			d = d.setDate(d.getDate() + 1);
			return new Date(d);
		}
	}

	function getStorage() {
		return storage;
	}

	return {
		reset: reset,
		getStorage: getStorage
	};
}

